"""
"开始游戏"界面模块
"""
import image
import window
import text
import pygame
import mouse
import process

# 开始游戏按钮在 y 轴上的一个位置
START_GAME_BTN_Y = 500
# 开始游戏按钮加载动画执行所需的时间，单位为帧
LOADING_DURATION = 120
# 开始游戏按钮加载动画已经执行的时间
loading_time = 0
# 开始游戏按钮草坪每帧增加的宽度
loading_grass_width_per_frame: float | int = 0
# 开始游戏按钮草卷每帧缩小的一个大小
loading_grass_roll_w_per_frame: float | int = 0
loading_grass_roll_h_per_frame: float | int = 0

# “开始游戏”按钮的图片对象
start_game_img_obj: image.Image | None = None


def start_game_page():
    """
    "开始游戏"界面
    :return:
    """
    global start_game_img_obj
    # 在游戏窗口中绘制"开始游戏"界面的背景图片
    image.draw_img(image.obj.start_game_bg, window.get_win())
    # 绘制植物大战僵尸 LOGO
    image.draw_img(image.obj.start_game_logo, window.get_win(), is_h_center=True)
    # 如果没有创建“开始游戏”按钮的图片对象
    if start_game_img_obj is None:
        # 创建"开始游戏"文本类对象
        start_game_text_obj = text.Text(
            '开始游戏',
            window.get_win(),
            (0, START_GAME_BTN_Y + 11),
            is_h_center=True,
            text_color='#D9B721',
            text_color_active='#FF5500',
            font_bold=True
        )
        # 创建“开始游戏”按钮的图片对象
        start_game_img_obj = image.Image(
            image.obj.start_game_btn_bar_dirt,
            window.get_win(),
            (0, START_GAME_BTN_Y),
            is_h_center=True,
            text_obj=start_game_text_obj
        )
        # 为“开始游戏”按钮的图片对象添加鼠标移动事件监听
        start_game_img_obj.add_listen_mouse_motion(start_game_img_obj_mouse_motion_handler)
    # 绘制开始游戏按钮
    start_game_img_obj.draw()
    # 开始游戏按钮加载动画
    loading()


def start_game_img_obj_mouse_motion_handler(is_mouse_motion: bool, event: pygame.event.Event):
    """
    鼠标在“开始游戏”按钮的图片上移动时的处理函数
    :param is_mouse_motion: 当前鼠标是否在“开始游戏”按钮的图片上移动
    :param event: 事件对象
    :return:
    """
    # 根据鼠标是否在“开始游戏”按钮的图片上移动，改变“开始游戏”按钮文本颜色和鼠标光标样式
    start_game_img_obj.text_obj.is_active = is_mouse_motion
    if is_mouse_motion:
        mouse.set_mouse_hand()
    else:
        mouse.set_mouse_arrow()


def start_game_img_obj_mouse_down_handler(event: pygame.event.Event):
    """
    鼠标在“开始游戏”按钮的图片上点击时的处理函数
    :param event: 事件对象
    :return:
    """
    process.set_current_process_name(process.MAIN_MENU)


def loading():
    """
    开始游戏按钮加载动画
    :return:
    """
    global loading_time
    global loading_grass_width_per_frame
    global loading_grass_roll_w_per_frame
    global loading_grass_roll_h_per_frame
    # 草坪图片对象
    grass_img_obj = image.obj.start_game_btn_bar_grass
    # 草坪在窗口的 x 轴上绘制的开始位置
    grass_init_x = window.get_win().get_width() / 2 - grass_img_obj.get_width() / 2 - 6
    # 草坪在游戏窗口中的绘制位置
    start_game_btn_bar_grass_position = (grass_init_x, START_GAME_BTN_Y - 20)
    # 草坪加载动画执行完成
    if loading_time == LOADING_DURATION:
        # 加载动画执行完成，修改按钮文本为“开始游戏”
        start_game_img_obj.text_obj.text = '开始游戏'
        # 为“开始游戏”按钮的图片对象添加鼠标带年纪事件监听
        start_game_img_obj.add_listen_mouse_down(start_game_img_obj_mouse_down_handler)
        # 绘制完整的草坪
        image.draw_img(grass_img_obj, window.get_win(), start_game_btn_bar_grass_position)
        return
    # 加载动画仍然在执行，按钮文本为“载入中...”
    start_game_img_obj.text_obj.text = '载入中...'
    # 计算每帧草坪绘制增加宽度
    if loading_grass_width_per_frame == 0:
        loading_grass_width_per_frame = grass_img_obj.get_width() / LOADING_DURATION
    # 绘制每一帧的草坪
    image.draw_img(
        grass_img_obj,
        window.get_win(),
        start_game_btn_bar_grass_position,
        (loading_time * loading_grass_width_per_frame, grass_img_obj.get_height())
    )
    # 草卷图片对象
    grass_roll_img_obj = image.obj.start_game_btn_bar_grass_roll
    # 计算每帧草卷宽高缩小的大小
    if loading_grass_roll_w_per_frame == 0:
        loading_grass_roll_w_per_frame = grass_roll_img_obj.get_width() / 3 / LOADING_DURATION
    if loading_grass_roll_h_per_frame == 0:
        loading_grass_roll_h_per_frame = grass_roll_img_obj.get_height() / 3 / LOADING_DURATION
    # 草卷图片的宽高
    grass_roll_img_w = grass_roll_img_obj.get_width()
    grass_roll_img_h = grass_roll_img_obj.get_height()
    # 草卷初始宽高大小
    grass_roll_init_w = grass_roll_img_w * 0.9
    grass_roll_init_h = grass_roll_img_h * 0.9
    # 绘制每一帧草卷
    image.draw_img(
        grass_roll_img_obj,
        window.get_win(),
        (
            grass_init_x + loading_time * loading_grass_width_per_frame - grass_roll_img_w / 2,
            START_GAME_BTN_Y - grass_roll_img_h * 3 / 4 + loading_time * loading_grass_roll_h_per_frame
        ),
        (
            grass_roll_init_w - loading_time * loading_grass_roll_w_per_frame,
            grass_roll_init_h - loading_time * loading_grass_roll_h_per_frame
        ),
        is_scale=True,
        rotate_degree=loading_time * -1
    )
    # 更新加载动画已经执行的时间
    loading_time += 1
